﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows;
using System.Collections.ObjectModel;

namespace for7raid.vkPlayer
{
    public static class Extensions
    {
        public static string Encode(this string text)
        {
            if (string.IsNullOrEmpty(text))
                return "";
            return text.Replace("&amp;", "&").Replace("&lt;", "<").Replace("&gt;", ">").Replace("&quot;", "\"").Replace("&apos;", "'").Replace("&#39;", "'");

        }

        public static int ParseXmlNodeAsInt(this XmlNode node)
        {
            if (node != null)
                return int.Parse(node.InnerText);
            else
                return 0;
        }

        public static void ClearValue(this ObservableCollection<MediaItem> Items)
        {
            if (Items == null)
                Items = new ObservableCollection<MediaItem>();
            else
                Items.Clear();
        }
    }


    public static class WindowPositionHelperConfig
    {
        public static void SaveSize(this Window win)
        {
            Properties.Settings.Default.WindowPosition = win.RestoreBounds;
            if (win.SizeToContent == SizeToContent.Manual)
                Properties.Settings.Default.WindowSize = win.RestoreBounds;
            Properties.Settings.Default.Save();
        }

        public static void SetSize(this Window win)
        {
            Rect rect = Properties.Settings.Default.WindowPosition;
            Rect rectSize = Properties.Settings.Default.WindowSize;
            win.Top = rect.Top;
            win.Left = rect.Left;

            // Only restore the size for a manually sized
            // window.
            if (win.SizeToContent == SizeToContent.Manual && rectSize.Width > 0 && rectSize.Height > 0)
            {
                win.Width = rectSize.Width;
                win.Height = rectSize.Height;
            }

        }
    }
}
